package com.laolu.icon.utils;

import com.laolu.icon.exception.MyException;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

public class MyFile {
	
	/**
	 * 判断是否为文件
	 * @param path
	 * @return
	 */
	public boolean isFile(String path) {
		File file = new File(path);
		return file.isFile();
	}
	
	/**
	 * 判断是否为文件夹
	 * @param path
	 * @return
	 */
	public boolean isDirectory(String path) {
		File file = new File(path);
		return file.isDirectory();
	}
	
	/**
	 * 判断是否为隐藏文件
	 * @param path
	 * @return
	 */
	public boolean isHiddenFile(String path) {
		File file = new File(path);
		return file.isHidden();
	}
	
	/**
	 * 判断是否为系统文件
	 * @param path
	 * @return
	 */
	public boolean isSystemFile(String path) {
		try {
			return (boolean) Files.getAttribute(Paths.get(path), "dos:system");
		} catch (IOException ignored) {
		}
		return false;
	}
	
	
	/**
	 * 获取桌面所有文件列表（包括用户桌面与公共桌面）
	 * @return 桌面文件列表
	 * @throws MyException
	 */
	public List<String> getDesktopFileList() throws MyException {
		List<String> list = new LinkedList<>();
		try {
			list.addAll(getPublicDesktopFileList());
		} catch (NullPointerException ignored) {
		}
		try {
			list.addAll(getUserDesktopFileList());
		} catch (NullPointerException ignored) {
		}
		return list;
	}
	
	/**
	 * 获取当前用户桌面文件列表
	 * @return 桌面文件列表
	 * @throws MyException
	 */
	public List<String> getUserDesktopFileList() throws MyException {
		return getFileList(new MyFolder().getUserDesktopPath());
	}
	
	/**
	 * 获取公共桌面文件列表
	 * @return 公共桌面文件列表
	 * @throws MyException
	 */
	public List<String> getPublicDesktopFileList() throws MyException {
		return getFileList(new MyFolder().getPublicDesktopPath());
	}
	
	/**
	 * 获取指定文件夹下的文件列表
	 * @param path
	 * 		文件夹路径
	 * @return 指定文件夹下的文件列表
	 * @throws NullPointerException
	 */
	public List<String> getFileList(String path) throws NullPointerException {
		File file = new File(path);
		return Arrays.stream(Objects.requireNonNull(file.listFiles())).map(File::getAbsolutePath).toList();
	}
	
}
